﻿using UnityEngine;
using System.Collections;
using System;

/// 广点通广告
public class GdtAds : AdsBase
{
	#region  初始化数据

	public override void Init (params string[] key)
	{
		if (jo == null) {
			jo = new AndroidJavaObject ("com.gdt.Main");
			jo.CallStatic ("setCallBack", "TJSDK", "OnADCallBack");
		}
		if (key.Length > 0 && key [0] != null) {
			jo.CallStatic ("init", key [0]);
		} else {
			TJLogError ("初始化错误");
			return;
		}
		if (key.Length > 1 && key [1] != null) {
			jo.CallStatic ("initBanner", key [0], key [1], (int)BannerPos.down, (int)BannerwWidth.wrap);
		}
		if (key.Length > 2 && key [2] != null) {
			jo.CallStatic ("initInsert", key [0], key [2]);
		}
		if (key.Length > 3 && key [3] != null) {
			jo.CallStatic ("initIncentInsert", key [0], key [3]);
		}
		if (key.Length > 4 && key [4] != null) {
			jo.CallStatic ("initNative", key [0], key [4]);
		}
		if (key.Length > 5 && key [5] != null) {
			jo.CallStatic ("initSplash", key [0], key [5]);
		}
	}

	#endregion

	bool CheckState(){
		if (TJSDK.Instance.GetAdLocState () == AdLocation.AdLocState.UnUse
		    || TJSDK.Instance.GetAdLocState () == AdLocation.AdLocState.Open) {
			return true;
		} else if (TJSDK.Instance.GetAdLocState () == AdLocation.AdLocState.UnInit) {
			TJLog ("AdLoc UnInit");
			return false;
		} else if (TJSDK.Instance.GetAdLocState () == AdLocation.AdLocState.Close) {
			TJLog ("AdLoc Close");
			return false;
		} else {
			return false;
		}
	}


	#region  展示 横幅 插屏 原生 开屏

	public override void ShowBanner (Action<bool> ac)
	{
		TJLog ("展示 Banner 广告");
		if (CheckState ()) {
			bannerCallBack = ac;
			jo.CallStatic ("showBanner");
		}
	}

	public override void CloseBanner ()
	{
		TJLog ("关闭 Banner 广告");
		jo.CallStatic ("closeBanner");
	}

	public override void ShowInsert (Action<bool> ac)
	{
		TJLog ("展示插屏广告");
		if (CheckState ()) {
			insertCallBack = ac;
			jo.CallStatic ("showInsert");
		}
	}

	public override void ShowIncent (Action<bool> ac)
	{
		TJLog ("展示激励插屏广告");
		if (CheckState ()) {
			incentCallBack = ac;
			jo.CallStatic ("showIncentInsert");
		}
	}

	public override void ShowSplash (Action<bool> ac)
	{
		TJLog ("展示开屏广告");
		if (CheckState ()) {
			splashCallBack = ac;
			jo.CallStatic ("showSplash");
		}
	}

	public override void ShowNative (Action<bool> ac)
	{
		TJLog ("展示原生广告");
		if (CheckState ()) {
			nativeCallBack = ac;
			jo.CallStatic ("showNative");
		}
	}

	public override void CloseNative ()
	{
		TJLog ("关闭原生广告");
		jo.CallStatic ("closeNative");
	}

	#endregion

	#region 判断Banner和插屏是否加载好

	public override bool IsBannerReady ()
	{	
		TJLog ("检测Banner状态");
		if (!CheckState ()) {
			return false;
		}
		bool state = jo.CallStatic<bool> ("isBannerReady");
		TJLog (state ? "Banner加载好啦" : "Banner没加载好");

		return state;
	}

	public override bool IsInsertReady ()
	{
		TJLog ("检测插频状态");
		if (!CheckState ()) {
			return false;
		}
		bool state = jo.CallStatic<bool> ("isInsertReady");
		TJLog (state ? "插屏已加载" : "插屏没加载好");

		return state;
	}

	public override bool IsIncentReady ()
	{
		TJLog ("检测激励插频状态");
		if (!CheckState ()) {
			return false;
		}
		bool state = jo.CallStatic<bool> ("isIncentInsertReady");
		TJLog (state ? "激励插屏已加载" : "激励插屏没加载好");

		return state;
	}

	#endregion

	public override void OnADCallback (string str)
	{
		TJLog ("广告回调: " + str);
		string[] datas = str.Split ('|');
		switch (datas [0]) {
		case AdsTypes.banner:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到Banner广告");					break;
			case AdsStates.noAD:				TJLog ("无Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (false);
					bannerCallBack = null;
				}
				break;
			case AdsStates.exposure: 			TJLog ("曝光Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (true);
					bannerCallBack = null;
				}
				break;																
			case AdsStates.click: 				TJLog ("点击Banner广告"); 					break;
			case AdsStates.closed:				TJLog ("关闭Banner广告");					break;
			default:							TJLog ("其它Banner回调：" + datas [1]);		break;
			}
			break;
		case AdsTypes.insert:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到插屏广告");						break;
			case AdsStates.noAD:				TJLog ("无插屏广告");	
				if (insertCallBack != null) {
					insertCallBack (false);
					insertCallBack = null;
				}
				break;
			case AdsStates.open:				TJLog ("打开插屏广告");
				break;
			case AdsStates.exposure:			TJLog ("曝光插屏广告"); 						break;
			case AdsStates.click:				TJLog ("点击插屏广告");						break;
			case AdsStates.closed:				TJLog ("关闭插屏广告");
				if (insertCallBack != null) {
					insertCallBack (true);
					insertCallBack = null;
				}
				break;
			default:							TJLog ("其它插屏回调：" + datas [1]);			break;
			}
			break;
		case AdsTypes.incent:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到激励插屏广告");					break;
			case AdsStates.noAD:				TJLog ("无激励插屏广告");	
				if (incentCallBack != null) {
					incentCallBack (false);
					incentCallBack = null;
				}
				break;
			case AdsStates.open:				TJLog ("打开激励插屏广告");					break;
			case AdsStates.exposure:			TJLog ("曝光激励插屏广告"); 					break;
			case AdsStates.click:				TJLog ("点击激励插屏广告");					break;
			case AdsStates.closed:				TJLog ("关闭激励插屏广告");
				if (incentCallBack != null) {
					incentCallBack (true);
					incentCallBack = null;
				}
				break;
			default:							TJLog ("其它激励插屏回调：" + datas [1]);		break;
			}
			break;
		case AdsTypes.native:
			switch (datas [1]) {
			case AdsStates.recive:				TJLog ("收到原生广告");					break;
			case AdsStates.noAD:				TJLog ("无原生广告");
				if (nativeCallBack != null) {
					nativeCallBack (false);
					nativeCallBack = null;
				}
				break;
			case AdsStates.open:				TJLog ("打开原生广告");
				if (nativeCallBack != null) {
					nativeCallBack (true);
					nativeCallBack = null;
				}
				break;
			case AdsStates.exposure:			TJLog ("曝光原生广告"); 					break;
			case AdsStates.click:				TJLog ("点击原生广告");					break;
			case AdsStates.closed:				TJLog ("关闭原生广告");					break;
			case AdsStates.error:				TJLog ("原生广告错误：" + datas [2]);
				if (nativeCallBack != null) {
					nativeCallBack (false);
					nativeCallBack = null;
				}
				break;
			default:							TJLog ("其它原生回调：" + datas [1]);		break;
			}
			break;
		case AdsTypes.splash:
			switch (datas [1]) {
			case AdsStates.exposure:			TJLog ("曝光开屏广告");					break;
			case AdsStates.click:				TJLog ("点击开屏广告"); 					break;
			case AdsStates.closed:				TJLog ("关闭开屏广告");
				if (splashCallBack != null) {
					splashCallBack (true);
					splashCallBack = null;
				}
				break;
			case AdsStates.noAD:
				TJLog ("无开屏广告：" + datas [2]);
//				if (splashCallBack != null) {
//					splashCallBack (false);
//					splashCallBack = null;
//				}
				if (IsIncentReady ()) {
					ShowIncent (splashCallBack);
					splashCallBack = null;
				} else {
					if (splashCallBack != null) {
						splashCallBack (false);
						splashCallBack = null;
					}
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
}
